﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using System.Windows.Forms;
using DevExpress.XtraTab;
using DevExpress.Utils;
using DevExpress.XtraTab.ViewInfo;
using RFIDStockManagement.BLL;
using RFIDStockManagement.Client.Report;
using RFIDStockManagement.Client.IOStock;
namespace RFIDStockManagement.Client
{
    partial class MainForm
    {
        /// <summary>
        /// 绑定导航
        /// </summary>
        public void BindNavigationBar()
        {
            lblCurrentUser.Text = string.Format("欢迎您！{0}", _curUser.Name);

            NavBarItem barItem = new NavBarItem("供应商");
            barItem.Name = "Supplier";
            nbgBasic.ItemLinks.Add(barItem);

            barItem = new NavBarItem("计量单位组");
            barItem.Name = "MeasureGroup";
            nbgBasic.ItemLinks.Add(barItem);

            barItem = new NavBarItem("仓库");
            barItem.Name = "Stock";
            nbgBasic.ItemLinks.Add(barItem);

            barItem = new NavBarItem("物料");
            barItem.Name = "Item";
            nbgBasic.ItemLinks.Add(barItem);
            barItem = new NavBarItem("标签设置");
            barItem.Name = "WriteTag";
            nbgBasic.ItemLinks.Add(barItem);

            barItem = new NavBarItem("及时库存");
            barItem.Name = "Report_Inventory";
            nbgReport.ItemLinks.Add(barItem);

            barItem = new NavBarItem("盘点报告");
            barItem.Name = "Report_StockCheck";
            nbgReport.ItemLinks.Add(barItem);

            barItem = new NavBarItem("入库");
            barItem.Name = "Stock_InStock";
            nbgStock.ItemLinks.Add(barItem);

            barItem = new NavBarItem("出库");
            barItem.Name = "Stock_OutStock";
            nbgStock.ItemLinks.Add(barItem);

            barItem = new NavBarItem("盘点作业");
            barItem.Name = "Stock_Check_Opt";
            nbgStock.ItemLinks.Add(barItem);

            barItem = new NavBarItem("speedway配置");
            barItem.Name = "SystemCfg_Speedway";
            nbgSystemCfg.ItemLinks.Add(barItem);


            navBar.LinkClicked += new NavBarLinkEventHandler(navBar_LinkClicked);

            this.tabMainContent.TabPages[0].ShowCloseButton = DefaultBoolean.False;
        }
        private void navBar_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            XtraUserControl control;
            switch (e.Link.ItemName)
            {
                case "Supplier": //供应商
                    control = new SupplierList();
                    AddTabPage("Supplier", "供应商", control);
                    break;
                case "MeasureGroup": //计量单位组
                    control = new MeasureGroupList();
                    AddTabPage("MeasureGroup", "计量单位组", control);
                    break;
                case "Stock": //仓库
                    control = new StockList();
                    AddTabPage("Stock", "仓库", control);
                    break;
                case "Item":
                    control = new ItemList();
                    AddTabPage("Item", "物料", control);
                    break;
                case "WriteTag":
                    control = new WriteTag();
                    AddTabPage("WriteTag", "标签", control);
                    break;

                case "Report_Inventory":
                    ReportControl report = new ReportControl();
                    report.ThisReport = ReoportCreator.CreateDBReporter("inventory");
                    report.SetPlugIn(new InventoryReportPlugIn());
                    AddTabPage("Report_Inventory", "即时库存", report);
                    break;
                case "Report_StockCheck":
                    ReportControl report2 = new ReportControl();
                    report2.ThisReport = ReoportCreator.CreateDBReporter("total_stock_check");
                    report2.SetPlugIn(new StockCheckReportPlugIn());
                    AddTabPage("Report_StockCheck", "盘点报告", report2);
                    break;

                case "Stock_InStock":
                    control = new InStockList(this._curUser);
                    AddTabPage("Stock_InStock", "入库", control);
                    break;
                case "Stock_OutStock":
                    control = new OutStockList(this._curUser);
                    AddTabPage("Stock_OutStock", "出库", control);
                    break;
                case "Stock_Check_Opt":
                    control = new SotckCheckForm();
                    AddTabPage("Stock_Check_Opt", "盘点作业", control);
                    break;

                case "SystemCfg_Speedway":
                    control = new SpeedwayConfig();
                    AddTabPage("SystemCfg_Speedway", "speedway配置", control);
                    break;
            }
        }
        private Dictionary<string, XtraTabPage> m_dctTabPages = new Dictionary<string, XtraTabPage>();
        /// <summary>
        /// 添加页签
        /// </summary>
        private void AddTabPage(string strName, string strCapture, XtraUserControl control)
        {
            XtraTabPage page = null;
            if (m_dctTabPages.ContainsKey(strName) == false)
            {
                page = new XtraTabPage();
                page.ShowCloseButton = DefaultBoolean.True;
                page.Name = strName;
                page.Text = strCapture;
                control.Dock = DockStyle.Fill;
                page.Controls.Add(control);
                this.tabMainContent.TabPages.Add(page);
                m_dctTabPages[strName] = page;
            }
            else
            {
                page = m_dctTabPages[strName];
            }
            this.tabMainContent.SelectedTabPage = page;
        }
        private void tabMainContent_CloseButtonClick(object sender, EventArgs e)
        {
            ClosePageButtonEventArgs ce = e as ClosePageButtonEventArgs;
            if (ce != null)
            {
                XtraTabPage page = ce.Page as XtraTabPage;
                if (page != null)
                {
                    int index = this.tabMainContent.TabPages.IndexOf(page);
                    this.tabMainContent.TabPages.Remove(page);
                    m_dctTabPages.Remove(page.Name);
                    this.tabMainContent.SelectedTabPageIndex = index - 1;
                    switch (page.Name)
                    {
                        case "Stock_InStock":
                            InStockList inControl = page.Controls[page.Controls.Count - 1] as InStockList;
                            if (inControl != null)
                            {
                                inControl.Stop(true);
                            }
                            break;
                        case "Stock_OutStock":
                            OutStockList outControl = page.Controls[page.Controls.Count - 1] as OutStockList;
                            if (outControl != null)
                            {
                                outControl.Stop(true);
                            }
                            break;
                        case "Stock_Check_Opt":
                            SotckCheckForm stockCheckControl = page.Controls[page.Controls.Count - 1] as SotckCheckForm;
                            if (stockCheckControl != null)
                            {
                                stockCheckControl.StopReader(true);
                            }
                            break;
                        case "WriteTag":
                            WriteTag writeTagControl = page.Controls[page.Controls.Count - 1] as WriteTag;
                            if (writeTagControl != null)
                            {
                                writeTagControl.StopRead(true);
                            }
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 窗体大小变更处理
        /// </summary>
        private void FormSizeChanged()
        {
            tabMainContent.Width = this.Width - navBar.Width - 25;
            pnlMainContent.Height = this.Height - pnlTitle.Height - 40;
            lblCurrentUser.Left = this.Width - lblCurrentUser.Width - lnkLoginOut.Width - 30;
        }

    }
}
